<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use Session;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;
use Crypt;


class LoginController extends Controller
{
    //

    public function login()
	{
		return view('admin.login.login');
	}

    // public function login(Request $request)
    // {
    // 	$data = $request -> except('_token');
    // }



    public function dologin(Request $request)
    {
    	$data = $request -> except('_token');	
    	
    	//检查验证码是否正确
    	$code = Session::get('milkcaptcha');
    	// dd($code);
    	if($code != $data['captcha'])
    	{
    		return back() -> with(['info' => '验证码错误']);
    	}

    	 $res = DB::table('users') -> where('email',$data['email']) -> first();

    	 if(!$res)
    	 {
    	 	return back() -> with(['info' => '您输入的邮箱不存在']);
    	 }

    	 $password = $res -> password;
    	 //解除密码，让可以判断是否一致
    	 $password = Crypt::decrypt($password);

    	 // dd($password);
    	 //判断
    	if($data['password'] != $password)
    	{
    		return back() -> with(['info' => '您输入的邮箱或密码错误']);
    	}
    	// dd($res);
    	//存储
    	session(['master' => $res]);
    	// dd(session('master'));


    	return redirect('/admin/index') -> with(['info' => '登陆成功']);

    }

    //logout
    public function logout()
    {
    	session() -> flush();
    	var_dump(session('master')); 
 
    }





}
